/**
 * author wen
 * cunyu
 * email wjd_13@hotmail.com
 */
package com.cunyu.service;

import cn.tom.kit.StringUtil;
import com.cunyu.bean.Form;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 * 角色管理 service 层
 * @author win
 * 2019/04/19-14:49
 */
@Service
public class RoleService extends BaseService{

    @SuppressWarnings("unchecked")
    public Map<String,Map<String,Object>> getRoles(Integer role){
        String sql = "select * from muserrole order by pid,id" ;
        List<Map<String, Object>> list = getList(sql);
        Map<String,Map<String,Object>> table = new LinkedHashMap<>() ;

        Map<String,Object> other = new HashMap<>() ;
        other.put("id",-1) ;
        other.put("pname","操作权限") ;
        other.put("child",new ArrayList<>()) ;
        table.put("-1",other) ;

        Map<String,Object> parent = new HashMap<>() ;
        parent.put("id",0) ;
        parent.put("pname","一级菜单") ;
        parent.put("child",new ArrayList<>()) ;
        table.put("0",parent) ;
        // tree format
        for (Map<String,Object> unit: list){
            table.put(unit.get("id").toString() ,unit) ;
            if(role != null){
                Object roleObj = unit.get("role");
                String roleStr ;
                if(roleObj == null)
                    roleStr = "" ;
                else
                    roleStr = roleObj.toString();
                if(roleStr.contains("["+role+"]")){
                    unit.put("has",true);
                }else{
                    unit.put("has",false);
                }
            }
            String pid = unit.get("pid").toString();
            if(table.get(pid) == null){
                logger.error("unknown role id "+pid);
                continue ;
            }
            if(table.get(pid).get("child") == null){
                table.get(pid).put("child",new ArrayList<>()) ;
            }
            ((List)table.get(pid).get("child")).add(unit) ;
        }
        return table ; // new a tree
    }

    public Map<String, Object> getInfo(int iid) {
        String sql = "select * from muserrole where id = ? " ;
        return getMap(sql,iid) ;
    }

    public int updateRole(Form form) {
        String sql = "update muserrole set name = ?,pid = ? ,uri=?,img=?,state=? " +
                "where id = ?" ;
        exec(sql,form.getName(),Integer.valueOf(form.getPid()),form.getUri(),
                form.getImg(),Integer.valueOf(form.getState()),Integer.valueOf(form.getId())) ;
        return Integer.valueOf(form.getId()) ;
    }

    public int newRole(Form form) {
        if(!StringUtil.hasLength(form.getRole())){
            // 为null 不能在  role || "***" 这种操作正常拼接。
            form.setRole("");
        }
        String sql = "insert into muserrole (pid,name,uri,role,img,state)" +
                " values (?,?,?,?,?,?)" ;
        return insert(sql,Integer.valueOf(form.getPid()),form.getName(),form.getUri(),
                form.getRole(),form.getImg(),Integer.valueOf(form.getState())) ;
    }
}
